/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.model.geo;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import java.io.Serializable;

public class GQuadCurve extends QuadCurve2D.Double implements Serializable {
	
	private static final long serialVersionUID = 1L;
	private GPoint p1;
	private GPoint ctrl;
	private GPoint p2;
	private int label = 0;
	private int nParts = 1;
	private Color color = Color.black;
	private GPoint[] discrete;
	private boolean isFrame = true;
	
//*****************************************************************************
	
	public GQuadCurve() {
		this(0, 0, 0, 0, 0, 0);
	}
	
//*****************************************************************************
	
	public GQuadCurve(double x1, double y1, double cx, double cy, double x2, double y2) {
		this(new GPoint(x1, y1), new GPoint(cx, cy), new GPoint(x2, y2));
	}
	
//*****************************************************************************
	
	public GQuadCurve(GPoint p1, GPoint c, GPoint p2) {
		this.p1 = p1;
		this.ctrl = c;
		this.p2 = p2;
		this.discrete();
	}
	
//*****************************************************************************
	
	public Point2D getP1() {
		return p1;
	}
	
//*****************************************************************************
	
	public Point2D getP2() {
		return p2;
	}
	
//*****************************************************************************
	
	public Point2D getCtrl() {
		return ctrl;
	}
	
//*****************************************************************************
	
	public double getX1() {
		return p1.getX();
	}
	
//*****************************************************************************
	
	public double getY1() {
		return p1.getY();
	}
	
//*****************************************************************************
	
	public double getX2() {
		return p2.getX();
	}
	
//*****************************************************************************
	
	public double getY2() {
		return p2.getY();
	}
	
//*****************************************************************************
	
	public double getXCtrl() {
		return ctrl.getX();
	}
	
//*****************************************************************************
	
	public double getYCtrl() {
		return ctrl.getY();
	}
	
//*****************************************************************************
	
	public boolean equals(Object obj) {
		if (obj instanceof QuadCurve2D) {
			QuadCurve2D qc = (QuadCurve2D)obj;
			if ((qc.getP1().equals(this.getP1())) && (qc.getCtrlPt().equals(this.getCtrlPt()))
													&& (qc.getP2().equals(this.getP2()))) {
				return true;
			}
		}
		return false;
	}
	
//*****************************************************************************
	
	public void setLabel(int lb) {
		this.label = lb;
	}
	
//*****************************************************************************
	
	public int getLabel( ) {
		return label;
	}
	
//*****************************************************************************
	
	public void setNParts(int np) {
		this.nParts = np;
		this.discrete();
	}
	
//*****************************************************************************
	
	public int getNParts( ) {
		return nParts;
	}
	
//*****************************************************************************
	
	public void setDiscrete(GPoint[] discret) {
		this.discrete = (GPoint[])discret.clone();
	}
	
//*****************************************************************************
	
	private void discrete( ) {
		double nx, ny, t, n;
		double p1x, p1y, p2x, p2y, ctrlx, ctrly;
		p1x = getX1();
		p1y = getY1();
		p2x = getX2();
		p2y = getY2();
		ctrlx = getXCtrl();
		ctrly = getYCtrl();
		discrete = new GPoint[nParts+1];
		for (int count = 0; count <= nParts; count++) {
			t = (double)count/nParts;
//			t = Math.sqrt(t);
//			nx = -p1x*Math.pow(t,2)+p1x+Math.pow(t,2)*p2x-2*Math.pow(t,2)+2*t;
//			ny = -p1y*Math.pow(t,2)+p1y+Math.pow(t,2)*p2y-2*Math.pow(t,2)+2*t;

//			primeira (a)
		n = 2;
		nx = -p1x*Math.pow(t,2)+p1x+Math.pow(t,2)*p2x-n*Math.pow(t,2)*(ctrlx-p1x)+n*t*(ctrlx-p1x);
		ny = -p1y*Math.pow(t,2)+p1y+Math.pow(t,2)*p2y-n*Math.pow(t,2)*(ctrly-p1y)+n*t*(ctrly-p1y);
		
			discrete[count] = new GPoint(nx,ny);
		}
	}
	
//*****************************************************************************
	
	public GPoint[] getDiscrete( ) {
		return discrete;
	}
	
//*****************************************************************************
	
	public void setColor(Color c) {
		this.color = c;
	}
	
//*****************************************************************************
	
	public Color getColor( ) {
		return color;
	}
	
//*****************************************************************************
	
	public double getAngle( ) {
		double angle = Math.atan2( (getY2()-getYCtrl()), (getX2()-getXCtrl()) );
		return angle;
	}
	
//*****************************************************************************
	
	public void setIsFrame(boolean a){
		isFrame = a;
	}
	
//*****************************************************************************
	
	public boolean isFrame(){
		return isFrame;
	}
	
//*****************************************************************************
}
